<template>
  <div class="text-center">
    <h2>用户登陆</h2>
    <el-form
      class="demo-ruleForm"
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
    >
      <el-form-item label="用户名称" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item label="选择房间" prop="room">
        <el-select v-model="ruleForm.room" placeholder="请选择房间">
          <el-option
            v-for="(item, index) in rooms"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-button type="primary" @click="submitForm('ruleForm')"
          >登陆</el-button
        >
      </el-row>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      ruleForm: {
        name: "",
        room: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入您的名称", trigger: "blur" },
          { min: 1, max: 20, message: "长度在 1 到 20 个字符", trigger: "blur" },
        ],
        room: [{ required: true, message: "请选择房间", trigger: "change" }],
      },
      //   房间集合
      rooms: [
        { label: "房间一", value: "room1" },
        { label: "房间二", value: "room2" },
        { label: "房间三", value: "room3" },
      ],
    };
  },
  mounted() {},
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.message('登陆成功，正在为您跳转！')
          setTimeout(()=>{
              this.toPage('/webRTC')
          },1000)
        } else {
          this.message("请填写完整！", "warning");
          return false;
        }
      });
    },
    toPage(path){
        this.$router.push({
            path:path,
            query:{
                userName:this.ruleForm.name,
                roomName:this.ruleForm.room
            }
        })
    },
    message(msg, type) {
      this.$message({
        message: msg || "错误！",
        type: type ? type : "success",
        duration:1000
      });
    },
  },
};
</script>
<style scope>
.text-center {
  text-align: center;
  padding: 15vh 0 0 0;
}
.demo-ruleForm {
  padding: 30px;
  display: inline-block;
  text-align: left;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border-radius: 4px;
}
</style>
